// https://unocss.nodejs.cn/guide/config-file
import {
  defineConfig,
  presetAttributify,
  presetIcons,
  presetTypography,
  presetUno,
  presetWebFonts,
  transformerDirectives,
  transformerVariantGroup,
} from "unocss";

import { FileSystemIconLoader } from "@iconify/utils/lib/loader/node-loaders";
import fs from "fs";

// 本地SVG图标目录
const iconsDir = "./src/assets/icons";

// 读取本地 SVG 目录，自动生成 safelist
const generateSafeList = () => {
  try {
    return fs
      .readdirSync(iconsDir)
      .filter((file) => file.endsWith(".svg"))
      .map((file) => `i-svg:${file.replace(".svg", "")}`);
  } catch (error) {
    console.error("无法读取图标目录:", error);
    return [];
  }
};

export default defineConfig({
  rules: [
    ["rlt-full", { position: "relative", width: "100%", height: "100%" }],
    ["abs-full", { position: "absolute", width: "100%", height: "100%" }],
    [
      "abs-center",
      { position: "absolute", top: "50%", left: "50%", transform: "translate(-50%, -50%)" },
    ],
    ["rlt-center", { position: "relative", "text-align": "center", "align-content": "center" }],
    ["rlt", { position: "relative" }],
    ["abs", { position: "absolute" }],
    /*
     * px集合
     */
    // bd-rd-${num}-px 定义border-radius
    // eslint-disable-next-line
    [/^bd-rd-([.\d]+)-px$/, ([_, num]) => ({ "border-radius": `${num}px` })],
    // m-${num}-px 定义margin
    // eslint-disable-next-line
    [/^m-([.\d]+)-px$/, ([_, num]) => ({ margin: `${num}px` })],
    // ml-${num}-px 定义margin-left
    // eslint-disable-next-line
    [/^ml-([.\d]+)-px$/, ([_, num]) => ({ "margin-left": `${num}px` })],
    // mr-${num}-px 定义margin-right
    // eslint-disable-next-line
    [/^mr-([.\d]+)-px$/, ([_, num]) => ({ "margin-right": `${num}px` })],
    // mt-${num}-px 定义margin-top
    // eslint-disable-next-line
    [/^mt-([.\d]+)-px$/, ([_, num]) => ({ "margin-top": `${num}px` })],
    // mb-${num}-px 定义margin-bottom
    // eslint-disable-next-line
    [/^mb-([.\d]+)-px$/, ([_, num]) => ({ "margin-bottom": `${num}px` })],
    // m-${num}-${num}-px 定义margin
    // eslint-disable-next-line
    [/^m-([.\d]+)-([.\d]+)-px$/, ([_, num1, num2]) => ({ margin: `${num1}px ${num2}px` })],
    // m-${num}-${num}-${num}-${num}-px 定义margin的四个值
    [
      /^m-([.\d]+)-([.\d]+)-([.\d]+)-([.\d]+)-px$/,
      // eslint-disable-next-line
      ([_, num1, num2, num3, num4]) => ({ margin: `${num1}px ${num2}px ${num3}px ${num4}px` }),
    ],
    // p-${num}-px 定义padding
    // eslint-disable-next-line
    [/^p-([.\d]+)-px$/, ([_, num]) => ({ padding: `${num}px` })],
    // pl-${num}-px 定义padding-left
    // eslint-disable-next-line
    [/^pl-([.\d]+)-px$/, ([_, num]) => ({ "padding-left": `${num}px` })],
    // pr-${num}-px 定义padding-right
    // eslint-disable-next-line
    [/^pr-([.\d]+)-px$/, ([_, num]) => ({ "padding-right": `${num}px` })],
    // pt-${num}-px 定义padding-top
    // eslint-disable-next-line
    [/^pt-([.\d]+)-px$/, ([_, num]) => ({ "padding-top": `${num}px` })],
    // pb-${num} 定义padding-bottom
    // eslint-disable-next-line
    [/^pb-([.\d]+)-px$/, ([_, num]) => ({ "padding-bottom": `${num}px` })],
    // p-${num}-${num}-px 定义padding
    // eslint-disable-next-line
    [/^p-([.\d]+)-([.\d]+)-px$/, ([_, num1, num2]) => ({ padding: `${num1}px ${num2}px` })],
    // p-${num}-${num}-${num}-${num}-px 定义padding的四个值
    [
      /^p-([.\d]+)-([.\d]+)-([.\d]+)-([.\d]+)-px$/,
      // eslint-disable-next-line
      ([_, num1, num2, num3, num4]) => ({ padding: `${num1}px ${num2}px ${num3}px ${num4}px` }),
    ],
    // w-${num}-h-${num} 定义宽高
    [
      /^w-([.\d]+)-h-([.\d]+)-px$/,
      // eslint-disable-next-line
      ([_, num1, num2]) => ({ width: `${num1}px`, height: `${num2}px` }),
    ],
    /*
     * rem集合
     */
    // bd-rd-${num}-rem 定义border-radius
    // eslint-disable-next-line
    [/^bd-rd-([.\d]+)-rem$/, ([_, num]) => ({ "border-radius": `${num}rem` })],
    // m-${num}-rem 定义margin
    // eslint-disable-next-line
    [/^m-([.\d]+)-rem$/, ([_, num]) => ({ margin: `${num}rem` })],
    // ml-${num}-rem 定义margin-left
    // eslint-disable-next-line
    [/^ml-([.\d]+)-rem$/, ([_, num]) => ({ "margin-left": `${num}rem` })],
    // mr-${num}-rem 定义margin-right
    // eslint-disable-next-line
    [/^mr-([.\d]+)-rem$/, ([_, num]) => ({ "margin-right": `${num}rem` })],
    // mt-${num}-rem 定义margin-top
    // eslint-disable-next-line
    [/^mt-([.\d]+)-rem$/, ([_, num]) => ({ "margin-top": `${num}rem` })],
    // mb-${num}-rem 定义margin-bottom
    // eslint-disable-next-line
    [/^mb-([.\d]+)-rem$/, ([_, num]) => ({ "margin-bottom": `${num}rem` })],
    // m-${num}-${num}-rem 定义margin
    // eslint-disable-next-line
    [/^m-([.\d]+)-([.\d]+)-rem$/, ([_, num1, num2]) => ({ margin: `${num1}rem ${num2}rem` })],
    // m-${num}-${num}-${num}-${num}-rem 定义margin的四个值
    [
      /^m-([.\d]+)-([.\d]+)-([.\d]+)-([.\d]+)-rem$/,
      // eslint-disable-next-line
      ([_, num1, num2, num3, num4]) => ({ margin: `${num1}rem ${num2}rem ${num3}rem ${num4}rem` }),
    ],
    // p-${num}-rem 定义padding
    // eslint-disable-next-line
    [/^p-([.\d]+)-rem$/, ([_, num]) => ({ padding: `${num}rem` })],
    // pl-${num}-rem 定义padding-left
    // eslint-disable-next-line
    [/^pl-([.\d]+)-rem$/, ([_, num]) => ({ "padding-left": `${num}rem` })],
    // pr-${num}-rem 定义padding-right
    // eslint-disable-next-line
    [/^pr-([.\d]+)-rem$/, ([_, num]) => ({ "padding-right": `${num}rem` })],
    // pt-${num}-rem 定义padding-top
    // eslint-disable-next-line
    [/^pt-([.\d]+)-rem$/, ([_, num]) => ({ "padding-top": `${num}rem` })],
    // pb-${num}-rem 定义padding-bottom
    // eslint-disable-next-line
    [/^pb-([.\d]+)-rem$/, ([_, num]) => ({ "padding-bottom": `${num}rem` })],
    // p-${num}-${num}-rem 定义padding
    // eslint-disable-next-line
    [/^p-([.\d]+)-([.\d]+)-rem$/, ([_, num1, num2]) => ({ padding: `${num1}rem ${num2}rem` })],
    // p-${num}-${num}-${num}-${num}-rem 定义padding的四个值
    [
      /^p-([.\d]+)-([.\d]+)-([.\d]+)-([.\d]+)-rem$/,
      // eslint-disable-next-line
      ([_, num1, num2, num3, num4]) => ({ padding: `${num1}rem ${num2}rem ${num3}rem ${num4}rem` }),
    ],
    // w${num}-h${num}-rem 定义宽高
    [
      /^w-([.\d]+)-h-([.\d]+)-rem$/,
      // eslint-disable-next-line
      ([_, num1, num2]) => ({ width: `${num1}rem`, height: `${num2}rem` }),
    ],
  ],
  // 自定义快捷类
  shortcuts: {
    "wh-full": "w-full h-full",
    "flex-center": "flex justify-center items-center",
    "flex-x-center": "flex justify-center",
    "flex-y-center": "flex items-center",
    "flex-x-start": "flex items-center justify-start",
    "flex-x-between": "flex items-center justify-between",
    "flex-x-end": "flex items-center justify-end",
  },
  theme: {
    colors: {
      primary: "var(--el-color-primary)",
      primary_dark: "var(--el-color-primary-light-5)",
    },
  },
  presets: [
    presetUno(),
    presetAttributify(),
    presetIcons({
      // 额外属性
      extraProperties: {
        display: "inline-block",
        width: "1em",
        height: "1em",
      },
      // 图表集合
      collections: {
        // svg 是图标集合名称，使用 `i-svg:图标名` 调用
        svg: FileSystemIconLoader(iconsDir, (svg) => {
          // 如果 `fill` 没有定义，则添加 `fill="currentColor"`
          return svg.includes('fill="') ? svg : svg.replace(/^<svg /, '<svg fill="currentColor" ');
        }),
      },
    }),
    presetTypography(),
    presetWebFonts({
      fonts: {
        // ...
      },
    }),
  ],
  safelist: generateSafeList(),
  transformers: [transformerDirectives(), transformerVariantGroup()],
});
